home *** CD-ROM | disk | FTP | other *** search
- /*
- File: GXEditValidation.c
-
- Contains:
-
- Written by: Barton R. House
-
- Copyright: © 1993 by Apple Computer, Inc., All rights reserved.
-
- */
-
- #include <Memory.h>
-
- #include "GXEdit.h"
- #include "GXEditDoc.h"
- #include "GXEditValidation.h"
- #include "GXEditError.h"
- #include "GXEditNewRun.h"
- #include "GXEditDebug.h"
-
- #define gxEditFullValidation GXEditDebug
-
- static Boolean CheckMagic(DocPtr dp);
- static Boolean CheckDoc(DocPtr dp);
- static Boolean CheckParagraph(DocPtr dp, ParaPtr pp);
- static Boolean CheckRun(DocPtr dp, NewRunPtr rp);
- static Boolean CheckRefCount(DocPtr dp);
-
- Boolean ValidateDoc(DocPtr dp)
- {
- return(CheckDoc(dp));
- }
-
- static Boolean CheckMagic(DocPtr dp)
- {
- if(dp->magic != kGXEditMagic)
- gxEditPostError(nil, gx_edit_internal_fatal_error);
-
- return(true);
- }
-
- static Boolean CheckRun(DocPtr dp, NewRunPtr rp)
- {
- if(rp->styleIndex < 0 || rp->styleIndex >= dp->numStyles) {
- gxEditPostError(nil, gx_edit_internal_fatal_error);
- return(false);
- }
-
- if(rp->numText == 0)
- gxEditPostError(dp, gx_edit_warning);
-
- return(true);
- }
-
- static Boolean CheckParagraph(DocPtr dp, ParaPtr pp)
- {
- int runIndex;
- NewRunPtr rp;
- Boolean ok = true;
-
- HLock((Handle) pp->runs);
- rp = *pp->runs;
-
- for(runIndex = 0; runIndex < pp->numRuns; runIndex++, rp++) {
-
- if(!CheckRun(dp, rp)) {
- ok = false;
- break;
- }
-
- if(runIndex != 0 && (rp->styleIndex == (rp-1)->styleIndex))
- gxEditPostError(dp, gx_edit_warning);
-
- }
-
- HUnlock((Handle) pp->runs);
-
- return(ok);
- }
-
- static Boolean CheckRefCount(DocPtr dp)
- {
- ParaPtr pp;
- StylePtr sp;
- long runCount;
- long styleCount;
- long scrapCount;
- short paraIndex;
- short styleIndex;
-
- runCount = 0;
- styleCount = 0;
-
- scrapCount = dp->scrap.numRuns;
-
- pp = *dp->paragraphs;
-
- for(paraIndex = 0; paraIndex < dp->numParagraphs; paraIndex++, pp++)
- runCount += pp->numRuns;
-
- sp = *dp->styles;
-
- for(styleIndex = 0; styleIndex < dp->numStyles; styleIndex++, sp++)
- styleCount += sp->refCount;
-
- if(styleCount != (runCount + scrapCount)) {
- gxEditPostError(nil, gx_edit_internal_fatal_error);
- return(false);
- }
-
- return(true);
-
- }
-
- static Boolean CheckDoc(DocPtr dp)
- {
- Boolean ok = true;
-
- /* for now, just check the magic number */
-
- if(!CheckMagic(dp))
- return(false);
-
- #if gxEditFullValidation
-
- HLock((Handle) dp->paragraphs);
-
- pp = *dp->paragraphs;
-
- for(paraIndex = 0; paraIndex < dp->numParagraphs; paraIndex++, pp++)
- if(!CheckParagraph(dp, pp)) {
- ok = false;
- break;
- }
-
- if(ok) ok = CheckRefCount(dp);
-
- HUnlock((Handle) dp->paragraphs);
-
- #endif
-
- return(ok);
- }
-